package com.hktf.corelib.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

public class NetWorkUtils {

	/**
	 * 检测是否联网
	 * @param context
	 * @return
	 */
	public static boolean isConnectingToInternet(Context context){
		
		if(null != context){
			
			ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);//获得系统服务
			if(manager != null){
				NetworkInfo[] info = manager.getAllNetworkInfo();
				NetworkInfo netWorkInfo = null;
				if(info != null){
					for(int i = 0;i < info.length; i++){
						if(info[i].getState() == NetworkInfo.State.CONNECTED){
							netWorkInfo = info[i];
							if(netWorkInfo.getType() == ConnectivityManager.TYPE_WIFI){
								return true;
							}else if(netWorkInfo.getType() == ConnectivityManager.TYPE_MOBILE){
								return true;
							}
						}
					}
				}
			}
		}
		
		return false;
	}
	
	 /**
     * 获取当前的网络类型
     * 
     * @return 1:wifi 0gprs   -1除了wifi以外的网络或没有网络
     */
    public static int getNetworkType(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if(networkInfo!=null){
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {// wifi
                return ConnectivityManager.TYPE_WIFI;
            } else if(networkInfo.getType() == ConnectivityManager.TYPE_MOBILE){
                return ConnectivityManager.TYPE_MOBILE;
            }else{
                return -1;
            }
        }
        return -1;
        
    }

    /**
     * 通过是否可以获取到sim卡串号判断  是否可以通话
     * @param context
     * @return
     */
    public  static boolean telPhoneisCanUse(){
        TelephonyManager tm=(TelephonyManager) CommUtils.getContext().getSystemService(Context.TELEPHONY_SERVICE);
        String simser = tm.getSimSerialNumber();
        if(simser!=null){
	  return true;
        }
        return false;
    }

}
